package xyz.mumuwei.youthcourseserver.job;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import xyz.mumuwei.youthcourseserver.common.lang.Const;
import xyz.mumuwei.youthcourseserver.mapper.OrderMapper;
import xyz.mumuwei.youthcourseserver.pojo.Order;

/**
 * 订单状态定时查询任务
 * 到达设定值对订单状态进行维护
 * 若订单未支付，则改变订单状态为已取消
 * 若订单已支付或退款等 不做任何操作
 * @author mumuwei
 * @date 2022/4/16
 */
@Slf4j
@RequiredArgsConstructor
public class OrderStateRefreshJob extends QuartzJobBean {

    final OrderMapper orderMapper;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        JobDataMap dataMap = context.getMergedJobDataMap();
        Integer id = dataMap.getInt("orderId");
        Order order = orderMapper.selectById(id);
        if (order != null && order.getState().equals(Const.ORDER_STATE_WAIT)) {
            order.setState(Const.ORDER_STATE_CANCLE);
            int i = orderMapper.updateById(order);
            if (i > 0) {
                log.info("订单id为{}，订单支付超时，已取消", id);
            }
        }
    }
}
